package com.dstats.fragments;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ProgressBar;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.adapter.TwitchAdapter;
import com.dstats.R;
import com.dstats.R.id;
import com.dstats.R.layout;
import com.entities.Twitch;
import com.fetch.Fetch;

@SuppressWarnings("unused")
public class TwitchFragment extends Fragment {

	 private ListView listView;
	 private ProgressBar progressBarNewsFragment;
	 
	 @Override
	public void onResume() {
		super.onResume();
		new FetchTwitch().execute();
	}
	 
	 private void setAdapterAndListener(List<Twitch> list){
		 if(list!=null){
		 listView.setAdapter(new TwitchAdapter(getActivity(), list));
			
			listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> adapter, View view, int arg2,
						long arg3) {
					
					Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(((Twitch) adapter.getItemAtPosition(arg2)).getUrl()));
					startActivity(browserIntent);
				}
				
			});
		 }
	 }
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
		             Bundle savedInstanceState) {
				
		        View rootView = inflater.inflate(R.layout.news_fragment, container, false);
		        
		        listView = (ListView) rootView.findViewById(R.id.listViewNews);
		        progressBarNewsFragment = (ProgressBar) rootView.findViewById(R.id.progressBarNewsFragment);
		        setAdapterAndListener(Twitch.listTwich);
		        if(Twitch.listTwich==null){
		        	new FetchTwitch().execute();
		        }
				
		        return rootView;
		}
		
		class FetchTwitch extends Fetch{

			@Override
			protected List<NameValuePair> getParams() {
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("game", "Dota 2"));

				return params;
			}

			@Override
			protected String getUrl() {

				return "https://api.twitch.tv/kraken/streams";
			}
			
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			progressBarNewsFragment.setVisibility(View.VISIBLE);
			progressBarNewsFragment.setIndeterminate(true);
		}
		@Override
		protected void onPostExecute(final JSONObject result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			getActivity().runOnUiThread(new Runnable() {

				@Override
				public void run() {
					setAdapterAndListener(Twitch.getLiveStreams(result));
					progressBarNewsFragment.setVisibility(View.INVISIBLE);
					
				}
			});
		}	
		
		}
		
	
}
